package com.blustreaksw.cyclecoach.settings;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import com.blustreaksw.cyclecoach.util.ItemDetailFragment;
import com.blustreaksw.cyclecoach.util.ItemListActivity;
import com.example.homescreendemosidemenu.R;

public class SettingsListActivity extends ItemListActivity {
	
	private final String SP_SETTINGS_LIST_ACTIVITY_LAST_FRAGMENT = 
			"com.blustreaksw.cyclecoach.settings_list_activity_last_fragment";
	private final String SP_SETTINGS_LIST_ACTIVITY = 
			"com.blustreaksw.cyclecoach.settings_list_activity";
	private final String EXTRAS_TARGET_FRAGMENT = 
			"com.blustreaksw.cyclecoach.extras_target_fragment";
	
	private final String PROFILE_SETTINGS_FRAGMENT = "ProfileSettingsFragment";
	private final String DEVICE_SETTINGS_FRAGMENT = "DeviceSettingsFragment";
	private final String DATA_EXPORT_SETTINGS_FRAGMENT = "DataExportSettingsFragment";
	
	private final String SETTINGS_LIST_ACTIVITY_USER_NAME = "com.blustreaksw.cyclecoach.settings_list_activity_user_name";
	
	private String mActiveFragment;
	
	private String mUserName;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		final Intent intent = getIntent();
        if((mActiveFragment = intent.getStringExtra(EXTRAS_TARGET_FRAGMENT)) == null) {
			restoreValuesFromSharedPreferences();
			if(mActiveFragment == null || mActiveFragment.equals("")) {
				mActiveFragment = PROFILE_SETTINGS_FRAGMENT;
				saveValuesToSharedPreferences();
			}
        }
        else {
        	saveValuesToSharedPreferences();
        }
		
		
		// TODO: If exposing deep links into your app, handle intents here.
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		restoreValuesFromSharedPreferences();
		rightPaneStartFragment(mActiveFragment);
		Log.d("SettingsListActivity", "onResume() called");
		
		// Test code
		mUserName = new String("tomas");
		
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		saveValuesToSharedPreferences();
		Log.d("SettingsListActivity", "onPause() called");
	}
	
	private void restoreValuesFromSharedPreferences() {
    	SharedPreferences sp = getSharedPreferences(SP_SETTINGS_LIST_ACTIVITY, MODE_PRIVATE);
    	mActiveFragment = sp.getString(SP_SETTINGS_LIST_ACTIVITY_LAST_FRAGMENT, null);
    	Log.d("SettingsListActivity", 
    			String.format("restoreValuesFromSharedPreferences() called loaded %s", mActiveFragment));
    	
    }
	
	private void saveValuesToSharedPreferences() {
		SharedPreferences sp = getSharedPreferences(SP_SETTINGS_LIST_ACTIVITY, MODE_PRIVATE);
		SharedPreferences.Editor editor = sp.edit();
		editor.putString(SP_SETTINGS_LIST_ACTIVITY_LAST_FRAGMENT, mActiveFragment);
		editor.commit();
		
		Log.d("SettingsListActivity", 
    			String.format("saveValuesToSharedPreferences() called saved %s", mActiveFragment));
	}
	
	@Override
	public void onItemSelected(String id) {
		if (isTwoPane()) {
			// In two-pane mode, show the detail view in this activity by
			// adding or replacing the detail fragment using a
			// fragment transaction.
			rightPaneStartFragment(id);

		} else {
			// In single-pane mode, simply start the detail activity
			// for the selected item ID.
			Intent detailIntent = new Intent(this, SettingsDetailActivity.class);
			detailIntent.putExtra(ItemDetailFragment.ARG_ITEM_ID, id);
			startActivity(detailIntent);
		}
	}
	
	private void rightPaneStartFragment(String id) {
		Bundle arguments = new Bundle();
		arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id);
		arguments.putString(SETTINGS_LIST_ACTIVITY_USER_NAME, mUserName);
		Log.d("ItemListActivity", String.format("onItemSelected id is %s", id));
		mActiveFragment = id;
		
		if (id.equals(PROFILE_SETTINGS_FRAGMENT)) {
			ProfileSettingsFragment fragment = new ProfileSettingsFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();
			
		} else if (id.equals(DEVICE_SETTINGS_FRAGMENT)) {
			DeviceSettingsFragment fragment = new DeviceSettingsFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();
			
		} else if (id.equals(DATA_EXPORT_SETTINGS_FRAGMENT)) {
			DataExportSettingsFragment fragment = new DataExportSettingsFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();
		} else {
			
			ItemDetailFragment fragment = new ItemDetailFragment();
			fragment.setArguments(arguments);
			getSupportFragmentManager().beginTransaction()
					.replace(R.id.item_detail_container, fragment).commit();
		}
	}
}
